package grafica;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;

public class VentanaGestion extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JList listado = null;
	private JLabel nombreLabel = null;
	private JLabel passwordLabel = null;
	private JLabel repasswordLabel = null;
	private JTextField nombre = null;
	private JButton crear = null;
	private JButton borrar = null;
	private JPasswordField password = null;
	private JPasswordField repassword = null;
	private JButton cerrar = null;
	private DefaultListModel listModel = new DefaultListModel();
	private ControladorGestion controller = null;
	private ArrayList<String> usuarios = null;
	/**
	 * This is the default constructor
	 */
	public VentanaGestion() {
		super();
		try {
			controller = new ControladorGestion();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(VentanaGestion.this, e.getMessage(), "Error del sistema", JOptionPane.WARNING_MESSAGE);
		}
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		if (controller!=null) {
			try {
				usuarios = controller.obtenerUsuarios();
			} catch (Exception e) {
				JOptionPane.showMessageDialog(VentanaGestion.this, e.getMessage(), "Error del sistema", JOptionPane.WARNING_MESSAGE);
			}
		}
		this.setSize(512, 275);
		this.setContentPane(getJContentPane());
		this.setTitle("Gestion de usuarios");
		this.addWindowListener(new java.awt.event.WindowAdapter() 
		{
			public void windowClosing(java.awt.event.WindowEvent e) 
			{
				VentanaGestion.this.dispose();
				System.exit(0);
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			repasswordLabel = new JLabel();
			repasswordLabel.setBounds(new java.awt.Rectangle(15,120,154,18));
			repasswordLabel.setText("Repita el password:");
			passwordLabel = new JLabel();
			passwordLabel.setBounds(new java.awt.Rectangle(15,68,157,19));
			passwordLabel.setText("Password:");
			nombreLabel = new JLabel();
			nombreLabel.setBounds(new java.awt.Rectangle(15,15,156,20));
			nombreLabel.setText("Nombre de usuario:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getListado(), null);
			jContentPane.add(nombreLabel, null);
			jContentPane.add(passwordLabel, null);
			jContentPane.add(repasswordLabel, null);
			jContentPane.add(getNombre(), null);
			jContentPane.add(getCrear(), null);
			jContentPane.add(getBorrar(), null);
			jContentPane.add(getPassword(), null);
			jContentPane.add(getRepassword(), null);
			jContentPane.add(getCerrar(), null);
		}
		return jContentPane;
	}
	private void actualizarListado(ArrayList<String> _usuarios) {
		listModel.clear();
		if(usuarios != null) {
			Iterator<String> iterator = usuarios.iterator();
			while (iterator.hasNext()) {
				listModel.addElement(iterator.next());
			}
		}
	}
	
	/**
	 * This method initializes listado	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getListado() {
		if (listado == null) {
			listado = new JList(listModel);
			listado.setBounds(new java.awt.Rectangle(268,4,224,193));
			actualizarListado(usuarios);
		}
		return listado;
	}

	/**
	 * This method initializes nombre	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNombre() {
		if (nombre == null) {
			nombre = new JTextField();
			nombre.setBounds(new java.awt.Rectangle(15,40,222,23));
		}
		return nombre;
	}

	/**
	 * This method initializes crear	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCrear() {
		if (crear == null) {
			crear = new JButton();
			crear.setBounds(new java.awt.Rectangle(16,200,155,27));
			crear.setText("Guardar");
			crear.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//Chequeo los campos del form
					if (nombre.getText().trim().isEmpty()) {
						JOptionPane.showMessageDialog(jContentPane, "El nombre es requerido", "Aviso", JOptionPane.WARNING_MESSAGE);
					} else {
						if (password.getPassword().length == 0) {
							JOptionPane.showMessageDialog(jContentPane, "El password es requerido", "Aviso", JOptionPane.WARNING_MESSAGE);
						} else {
							if (repassword.getPassword().length == 0 || !Arrays.equals(repassword.getPassword(), password.getPassword())) {
								JOptionPane.showMessageDialog(jContentPane, "El password no coincide", "Aviso", JOptionPane.WARNING_MESSAGE);
							} else {
								try {
									String _nombre = nombre.getText().trim();
									String _password = new String(password.getPassword());
									nombre.setText(new String());
									password.setText(new String());
									repassword.setText(new String());
									controller.crearUsuario(_nombre, _password);
									JOptionPane.showMessageDialog(jContentPane, "El usuarios fue creado correctamente", "Aviso", JOptionPane.INFORMATION_MESSAGE);
								} catch (Exception e1) {
									JOptionPane.showMessageDialog(jContentPane, e1.getMessage(), "Error del sistema", JOptionPane.WARNING_MESSAGE);
								}
								try {
									usuarios = controller.obtenerUsuarios();
									actualizarListado(usuarios);
								} catch (Exception e1) {
									JOptionPane.showMessageDialog(jContentPane, e1.getMessage(), "Error del sistema", JOptionPane.WARNING_MESSAGE);
								}
							}
						}
					}
				}
			});
		}
		return crear;
	}

	/**
	 * This method initializes borrar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBorrar() {
		if (borrar == null) {
			borrar = new JButton();
			borrar.setBounds(new java.awt.Rectangle(176,200,155,27));
			borrar.setText("Eliminar");
			borrar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					if ( listado.getSelectedIndex() != -1 && !listado.getSelectedValue().toString().isEmpty()) {
						String _name = listado.getSelectedValue().toString();
						if (JOptionPane.showConfirmDialog(jContentPane, "Seguro que quiere borrar el usuario: "+_name)==0) {
							try {
								controller.eliminarUsuario(_name);
							} catch (Exception e1) {
								JOptionPane.showMessageDialog(jContentPane, e1.getMessage(), "Error del sistema", JOptionPane.WARNING_MESSAGE);
							}
							try {
								usuarios = controller.obtenerUsuarios();
								actualizarListado(usuarios);
							} catch (Exception e1) {
								JOptionPane.showMessageDialog(jContentPane, e1.getMessage(), "Error del sistema", JOptionPane.WARNING_MESSAGE);
							}
						}
					} else {
						JOptionPane.showMessageDialog(jContentPane, "Tiene que seleccionar un usuario de la lista", "Aviso", JOptionPane.WARNING_MESSAGE);
					}
				}
			});
		}
		return borrar;
	}

	/**
	 * This method initializes password	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getPassword() {
		if (password == null) {
			password = new JPasswordField();
			password.setBounds(new java.awt.Rectangle(15,91,222,23));
		}
		return password;
	}

	/**
	 * This method initializes repassword	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getRepassword() {
		if (repassword == null) {
			repassword = new JPasswordField();
			repassword.setBounds(new java.awt.Rectangle(15,144,222,23));
		}
		return repassword;
	}

	/**
	 * This method initializes cerrar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCerrar() {
		if (cerrar == null) {
			cerrar = new JButton();
			cerrar.setBounds(new java.awt.Rectangle(337,200,155,27));
			cerrar.setText("Cerrar");
			cerrar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return cerrar;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
